package com.action.school;

import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import com.action.base.BaseAction;
import com.opensymphony.xwork2.ModelDriven;
import com.pageModel.Json;
import com.pageModel.Teacher;
import com.service.school.TeacherServiceI;

/**
 * 教师ACTION
 * 
 * @author chenq
 * 
 */

@Namespace("/school")
@Action(value = "teacherAction")
public class TeacherAction extends BaseAction implements ModelDriven<Teacher>{

	Teacher teacher=new Teacher();
	
	@Override
	public Teacher getModel() {
		return teacher;
	}
	
	private TeacherServiceI TeacherService;

	public TeacherServiceI getTeacherService() {
		return TeacherService;
	}
	@Autowired
	public void setTeacherService(TeacherServiceI teacherService) {
		TeacherService = teacherService;
	}
	
	public void datagrid(){
		super.writeJson(TeacherService.datagrid(teacher));
	}

	public void remove(){
		TeacherService.remove(teacher.getIds());
		Json j = new Json();
		j.setSuccess(true);
		j.setMsg("删除成功！");
		super.writeJson(j);
	}
	
	public void add() {
		String json = null;
		Map<String, Object> m = new HashMap<String, Object>();
		try {
			boolean state=TeacherService.add(teacher);
			if (state) {
				m.put("success", true);
				m.put("msg", "添加成功!");
			} else {
				m.put("success", false);
				m.put("msg", "该教师已存在!");
			}
		} catch (Exception e) {
			e.printStackTrace();
			m.put("success", false);
			m.put("msg", "添加失败！");
			System.out.print(e.getMessage());
		}
		super.writeJson(m);
	}
	
	public void edit() {
		TeacherService.edit(teacher);
		Json j=new Json();
		j.setSuccess(true);
		j.setMsg("修改成功！");
		super.writeJson(j);
	}
}
